package com.qifenqian.turbot.module.model

import android.content.Context
import com.qifenqian.turbot.base.BaseModel
import com.qifenqian.turbot.module.contract.DishContract
import com.qifenqian.turbot.module.entity.request.CategoryRequest
import com.qifenqian.turbot.module.entity.request.DishAddEditRequest
import com.qifenqian.turbot.module.entity.response.UploadEntity
import com.qifenqian.turbot.module.entity.response.dish.CategoryAndDishEntity
import com.qifenqian.turbot.module.entity.response.dish.CategoryEntity
import com.qifenqian.turbot.module.entity.response.dish.DishEntity
import com.qifenqian.turbot.net.ModelCallback
import com.qifenqian.turbot.net.NetObserver
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

/**
 * @author H
 * @createTime 2018/12/27 14:25
 * @description
 * @see
 * @since
 */
class DishModel(context: Context) : BaseModel(context), DishContract.Model {

    override fun uploadFile(filePath: String, callback: ModelCallback<UploadEntity>) {
        mClient!!.uploadFile(filePath)
                .subscribe(object : NetObserver<UploadEntity>(mContext, true) {
                    override fun onSuccess(msg: String?, t: UploadEntity?) {
                        callback.onSuccess(msg, t)
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        callback.onFailure(code, msg)
                    }
                })
    }

    override fun editDish(request: DishAddEditRequest, callback: ModelCallback<Nothing>) {
        mClient!!.editDish(request)
                .subscribe(object : NetObserver<Nothing>(mContext, true) {
                    override fun onSuccess(msg: String?, t: Nothing?) {
                        callback.onSuccess(msg)
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        callback.onFailure(code, msg)
                    }
                })
    }

    override fun deleteDish(dishId: Int, callback: ModelCallback<Nothing>) {
        mClient!!.deleteDish(dishId)
                .subscribe(object : NetObserver<Nothing>(mContext, true) {
                    override fun onSuccess(msg: String?, t: Nothing?) {
                        callback.onSuccess(msg)
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        callback.onFailure(code, msg)
                    }
                })
    }

    override fun saveCategory(request: CategoryRequest, callback: ModelCallback<Nothing>) {
        mClient!!.saveCategory(request)
                .subscribe(object : NetObserver<Nothing>(mContext, true) {
                    override fun onSuccess(msg: String?, t: Nothing?) {
                        callback.onSuccess(msg)
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        callback.onFailure(code, msg)
                    }
                })
    }

    override fun deleteCategory(dishTypeId: Int, callback: ModelCallback<Nothing>) {
        mClient!!.deleteCategory(dishTypeId)
                .subscribe(object : NetObserver<Nothing>(mContext, true) {
                    override fun onSuccess(msg: String?, t: Nothing?) {
                        callback.onSuccess(msg)
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        callback.onFailure(code, msg)
                    }
                })
    }

    override fun editCategory(dishName: String, dishTypeId: Int, callback: ModelCallback<Nothing>) {
        mClient!!.editCategory(dishName, dishTypeId)
                .subscribe(object : NetObserver<Nothing>(mContext, true) {
                    override fun onSuccess(msg: String?, t: Nothing?) {
                        callback.onSuccess(msg)
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        callback.onFailure(code, msg)
                    }
                })
    }

    override fun addDish(request: DishAddEditRequest, callback: ModelCallback<DishEntity>) {
        mClient!!.addDish(request)
                .subscribe(object : NetObserver<DishEntity>(mContext, true) {
                    override fun onSuccess(msg: String?, t: DishEntity?) {
                        t!!.dishName = request.dishName
                        callback.onSuccess(msg, t)
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        callback.onFailure(code, msg)
                    }
                })
    }

    override fun addCategory(dishTypeName: String, callback: ModelCallback<CategoryEntity>) {
        mClient!!.addCategory(dishTypeName)
                .subscribe(object : NetObserver<CategoryEntity>(mContext, true) {
                    override fun onSuccess(msg: String?, t: CategoryEntity?) {
                        callback.onSuccess(msg, t)
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        callback.onFailure(code, msg)
                    }
                })
    }

    override fun getDishAndCategoryList(callback: ModelCallback<Any>) {
        val categoryAndDishEntity = CategoryAndDishEntity(null, null)
        Observable.merge(mClient!!.getCategoryList(), mClient!!.getAllDishList())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : NetObserver<Any>(mContext, true) {
                    override fun onSuccess(msg: String?, t: Any?) {
                        if (t is List<*>) {
                            for (i in t) {
                                if (i is CategoryEntity) {
                                    categoryAndDishEntity.categoryEntities = t as List<CategoryEntity>
                                } else if (i is DishEntity) {
                                    categoryAndDishEntity.dishEntities = t as List<DishEntity>
                                }
                            }
                        }
                        callback.onSuccess(msg, categoryAndDishEntity)
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        callback.onFailure(code, msg)
                    }

                })
    }

    //    override fun getAllDishList(callback: ModelCallback<List<DishEntity>>) {
//        mClient!!.getAllDishList()
//                .subscribe(object : NetObserver<List<DishEntity>>(mContext, true) {
//                    override fun onSuccess(msg: String?, t: List<DishEntity>?) {
//                        callback.onSuccess(t = t)
//                    }
//
//                    override fun onFailure(code: Int, msg: String?) {
//                        callback.onFailure(code, msg)
//                    }
//                })
//    }
//
    override fun getCategoryList(callback: ModelCallback<List<CategoryEntity>>) {
        mClient!!.getCategoryList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : NetObserver<List<CategoryEntity>>(mContext, true) {
                    override fun onSuccess(msg: String?, t: List<CategoryEntity>?) {
                        callback.onSuccess(msg, t)
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        callback.onFailure(code, msg)
                    }
                })
    }
}
